package com.fastCms.business.bim.manage.impl;

import com.fastCms.business.bim.daos.AirMapper;
import com.fastCms.business.bim.manage.WeatherManageService;
import com.fastCms.business.bim.pojo.entity.Air;
import com.fastCms.business.bim.pojo.po.ChartPO;
import com.fastCms.business.bim.pojo.vo.AirVO;
import com.fastCms.business.bim.pojo.vo.WeatherVO;
import com.fastCms.business.frame.response.ApiResponse;
import com.fastCms.business.frame.response.ResponseUtil;
import com.fastCms.business.frame.utils.BeanConvertUtil;
import com.fastCms.business.frame.utils.opc.DateUtil;
import com.fastCms.business.frame.utils.qweather.QweatherUtil;
import com.fastCms.business.frame.utils.qweather.response.QAirInfoResp;
import com.fastCms.business.frame.utils.qweather.response.QWeatherResp;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Transactional(rollbackFor = Exception.class)
@Component
@Slf4j
public class WeatherManageServiceImpl implements WeatherManageService {

    @Value("${qweather.key}")
    private String qweatherkey;

    @Value("${qweather.locationid}")
    private String locationid;

    @Autowired
    private AirMapper airMapper;

    @Override
    public ApiResponse<AirVO> getNowAir() {
        QAirInfoResp resp = QweatherUtil.airInfo(qweatherkey, locationid);
        if (null != resp) {
            AirVO nowAir = BeanConvertUtil.convertBean(resp.getNow(), AirVO.class);
            return ResponseUtil.success(nowAir);
        }
        return ResponseUtil.fail();
    }

    @Override
    public ApiResponse<WeatherVO> getNowWeather() {
        QWeatherResp qWeatherResp = QweatherUtil.weatherInfo(qweatherkey, locationid);
        if (null != qWeatherResp) {
            WeatherVO nowAir = BeanConvertUtil.convertBean(qWeatherResp.getNow(), WeatherVO.class);
            return ResponseUtil.success(nowAir);
        }
        return ResponseUtil.fail();
    }

    @Override
    public ApiResponse<WeatherVO> getNowWeatherHistory() {
        QWeatherResp qWeatherResp = QweatherUtil.weatherHisInfo(qweatherkey, locationid);
        if (null != qWeatherResp) {
            WeatherVO nowAir = BeanConvertUtil.convertBean(qWeatherResp.getNow(), WeatherVO.class);
            return ResponseUtil.success(nowAir);
        }
        return ResponseUtil.fail();
    }

    @Override
    public ApiResponse<List<Air>> getHisAir(ChartPO chartPO) {
        if (StringUtils.isEmpty(chartPO.getStartTime())) {
            chartPO.setStartTime(DateUtil.getPassDayS(1));
        }
        if (StringUtils.isEmpty(chartPO.getEndTime())) {
            chartPO.setEndTime(DateUtil.getCurrentDayE());
        }
        if (!chartPO.getEndTime().contains(":")) {
            chartPO.setEndTime(chartPO.getEndTime() + " 23:59:59");
        }
        List<Air> airs = airMapper.getHislist(chartPO);
        return ResponseUtil.success(airs);
    }
}

